Ext.Loader.setPath('Ext.ux', CDR.Constant.CONTEXT_PATH + '/resources/extjs/examples/ux');
Ext.require(['*']);
Ext.require([
     'Ext.ux.form.MultiSelect',
     'Ext.ux.form.ItemSelector'
 ]);
Ext.tip.QuickTipManager.init();

//create cutom vtype

Ext.define('CDR.Main', {
	extend:'Ext.Viewport',
	layout:'border',
	initComponent: function() {
		var me = this;
		me.callParent();
		//create our components
		me.root = Ext.create('widget.panel',{
			region: 'center',
			layout:'border',
			tbar : [{
				text: CDR.I18N['global.title'],
				iconCls: 'win-icon'
			},{xtype: 'tbfill'}
			,{
				text: 'Administrator',
				iconCls: 'icon-pbx',
				menu: {
					width: '100',
					xtype: 'menu',
					items: {
						text:'Logout',
						iconCls:'icon-logout',
						handler: function(){
							document.location.href = 'j_spring_security_logout';
						}
					}
				}
			}],
			padding:2,
			border:false
		});
		me.tabPanel = Ext.create('widget.tabpanel', {
			region: 'center',
		});
		//create tab
		//profile-tab
		me.profileTab = Ext.create('widget.panel', {
			layout:'border',
			title: CDR.I18N['profile.tab.title'],
			border: false,
			iconCls: 'tab-profile',
			padding: 3
		});
		var serviceProvider = Ext.createWidget('serviceprovider');
		var pbxFactory = Ext.createWidget('pbxfactory');
		var userFactory = Ext.createWidget('userfactory');
		var accordion = new Ext.panel.Panel({
			layout:'accordion',
			border: false,
			defaults: {
				// applied to each contained panel
				bodyStyle: 'padding:10px'
			},
			layoutConfig: {
				// layout-specific configs go here
				titleCollapse: false,
				animate: true,
				activeOnTop: true
			},
			items: [pbxFactory, serviceProvider, userFactory]
		});
		
		me.profileTab.west = Ext.create('widget.panel', {
			region:'west',
			collapsible: false,
			split: true,
			width: '20%',
			layout: 'fit',
			items: [accordion]
		});
		
		me.profileTab.add(me.profileTab.west);
		//center region
		
		me.profileTab.center = Ext.create('widget.container', {
			region:'center',
			layout: 'fit',
			items:[pbxFactory.pbx]
		});
		serviceProvider.profileCenter = me.profileTab.center;
		pbxFactory.profileCenter = me.profileTab.center;
		userFactory.profileCenter = me.profileTab.center;
		me.profileTab.add(me.profileTab.center);
		
		
		me.tabPanel.add(me.profileTab);
		
		//aaa-tab
		me.siteTab = Ext.create('widget.panel', {
			layout:'border',
			title:CDR.I18N['organization.tab.title'],
			padding: 3,
			items:[Ext.create('CDR.widget.Site', {region: 'center'})]
		});
		me.tabPanel.add(me.siteTab);
		me.tabPanel.setActiveTab(0);
		me.root.add(me.tabPanel);
		me.add(me.root);
	}
});

//create new win
var newProviderWin = Ext.create('widget.window', {
		title: CDR.I18N['profile.new.provider'],
		closable: true,
		closeAction: 'hide',
		//animateTarget: this,
		width: 375,
		height: 200,
		bodyStyle: 'padding: 5px;',
		items: [{
			xtype: 'form',
			url: 'create-provider',
			items:[{
				xtype: 'textfield',
				allowBlank: false,
				fieldLabel: CDR.I18N['profile.provider.form.name'],
				width:350,
				name: 'name'
			}, {
				xtype: 'textareafield',
				allowBlank: false,
				fieldLabel: CDR.I18N['profile.provider.form.description'],
				width:350,
				name: 'description'
			}],
			buttons: [{
				text: CDR.I18N['button.save'],
				formBind: true, 
				handler: function() {
					var form = this.up('form').getForm();
					if (form.isValid()) {
						form.submit({
							success: function(form, action) {
							   Ext.Msg.alert('Success', action.result.msg);
							   newProviderWin.hide();
							},
							failure: function(form, action) {
								Ext.Msg.alert('Failed', action.result.msg);
							}
						});
					}
				}
			},{
				text: CDR.I18N['button.cancel'],
				handler: function() {
					newProviderWin.hide();
				}
				
			}]
		}]
});
Ext.onReady(function() {
	mainApp = Ext.create('CDR.Main');
	/*Ext.each(CDR.toolTips, function(i){
		Ext.create('Ext.tip.ToolTip', i);
		console.log(i)
	});*/
	//Ext.QuickTips.init();
});